package com.zb.jnlxc.action;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.jbpm.api.ProcessInstance;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ZLHW.base.Form.Page;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.zb.jnlxc.form.MiniPageReq;
import com.zb.jnlxc.form.MiniPageRsp;
import com.zb.jnlxc.form.OrderDetailForm;
import com.zb.jnlxc.model.Admin;
import com.zb.jnlxc.model.OrderForm;
import com.zb.jnlxc.model.PaichanOrderDetail;
import com.zb.jnlxc.service.BusinessService;

/**
 * Created with IntelliJ IDEA.
 * User: root
 * Date: 12-12-29
 * Time: 下午3:27
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/BussinessAction")
@SessionAttributes(value = { "user" })
public class BussinessAction {

    @Resource
    BusinessService businessService;

    @ResponseBody
    @RequestMapping("/getOrderFormsByScheme")
    public MiniPageRsp getOrderFormsByScheme(Integer schemeId) {
        return new MiniPageRsp(businessService.getOrderFormsByScheme(schemeId));
    }

    //    @ResponseBody
    //    @RequestMapping("/getProductRecordsByScheme")
    //    public MiniPageRsp getProductRecordsByScheme(Integer schemeId) {
    //        return new MiniPageRsp(businessService.getProductRecordsByScheme(schemeId));
    //    }

    @ResponseBody
    @RequestMapping("/loadOrderForm")
    public MiniPageRsp loadOrderForm(MiniPageReq page, @ModelAttribute("user") Admin user,
                                     HttpServletRequest request) throws ParseException {
        page.setRequest(request);
        businessService.loadOrderForm(page, user);
        return new MiniPageRsp(page.getResultData(), page.getTotalClum());
    }

    @ResponseBody
    @RequestMapping("/loadOrderDetail")
    public MiniPageRsp loadOrderDetail(MiniPageReq page, @ModelAttribute("user") Admin user,
                                       HttpServletRequest request) throws ParseException {
        page.setRequest(request);
        businessService.loadOrderDetail(page, user);
        return new MiniPageRsp(page.getResultData(), page.getTotalClum());
    }

    @ResponseBody
    @RequestMapping("/getLastOrderForm")
    public OrderForm getLastOrderForm(Integer schemeId, Integer clientId) throws ParseException {
        return businessService.getLastOrderForm(schemeId, clientId);
    }

    @ResponseBody
    @RequestMapping("/getOrderDetail")
    public MiniPageRsp getOrderDetail(Integer orderId) {
        List data = businessService.getOrderDetail(orderId);
        return new MiniPageRsp(data, data.size());
    }

    @ResponseBody
    @RequestMapping("/getUnPanchanOrderDetail")
    public MiniPageRsp getUnPanchanOrderDetail(Integer orderId) {
        List data = businessService.getUnPanchanOrderDetail(orderId);
        return new MiniPageRsp(data, data.size());
    }

    @ResponseBody
    @RequestMapping("/getOrderDetailByScheme")
    public MiniPageRsp getOrderDetailByScheme(Integer SchemeId) {
        List data = businessService.getOrderDetailByScheme(SchemeId);
        return new MiniPageRsp(data);
    }

    @ResponseBody
    @RequestMapping("/getOrder")
    public OrderForm getOrder(Integer orderId) {
        return businessService.getById(orderId);
    }

    @ResponseBody
    @RequestMapping("/getOrderFormsByAgent")
    public Page getOrderFormsByAgent(Page page) {
        return businessService.getOrderFormsByAgent(page);
    }

    @ResponseBody
    @RequestMapping("/getCompleteOrderForms")
    public Page getCompleteOrderForms(Page page) {
        return businessService.getCompleteOrderForms(page);
    }

    @ResponseBody
    @RequestMapping("/loadAgents")
    public List loadAgents() {
        return businessService.loadAgents();
    }

    @ResponseBody
    @RequestMapping("/getAllAgents")
    public List getAllAgents() {
        return businessService.getAllAgents();
    }

    //    @ResponseBody
    //    @RequestMapping("/getProductRecordsByOrderForm")
    //    public List getProductRecordsByOrderForm(OrderForm orderForm) {
    //        return businessService.getProductRecordsByOrderForm(orderForm);
    //    }

    @ResponseBody
    @RequestMapping("/saveAuthorizedAgents")
    public void saveAuthorizedAgents(List<Admin> authorizedAgents,
                                     @ModelAttribute("user") Admin user) {
        businessService.saveAuthorizedAgents(authorizedAgents, user);
    }

    @ResponseBody
    @RequestMapping("/initOrderProxy")
    public Map initOrderProxy() {
        return businessService.initOrderProxy();
    }

    @ResponseBody
    @RequestMapping("/generateCode")
    public String generateCode(Integer clientId) {
        return businessService.generateCode(clientId);
    }

    @ResponseBody
    @RequestMapping("/saveUpdateOrder")
    public OrderForm saveUpdateOrder(String orderFormData, String orderDetailData, String operate,
                                     String taskId, @ModelAttribute("user") Admin user) {
        OrderForm orderForm = JSONObject.parseObject(orderFormData, OrderForm.class);
        List<OrderDetailForm> orderDetails = JSONObject.parseObject(orderDetailData.replaceAll("_state","state"),
            new TypeReference<List<OrderDetailForm>>() {
            }.getType());
        operate = operate == null ? "saveOrder" : operate;
        if ("saveOrder".equals(operate)) {
            return businessService.saveOrder(orderForm, orderDetails, user);
        } else if ("completeUpdateOrder".equals(operate)) {
            return businessService.completeUpdateOrder(taskId, orderForm, orderDetails, user);
        } else {
            return orderForm;
        }
    }

    @ResponseBody
    @RequestMapping("/saveSample")
    public OrderForm saveSample(String orderFormData, String orderDetailData, String taskId,
                                @ModelAttribute("user") Admin user) {
        OrderForm orderForm = JSONObject.parseObject(orderFormData, OrderForm.class);
        List<OrderDetailForm> orderDetails = JSONObject.parseObject(orderDetailData,
            new TypeReference<List<OrderDetailForm>>() {
            }.getType());
        return businessService.saveSample(orderForm, orderDetails, user, taskId);
    }

    @ResponseBody
    @RequestMapping("/getOrderFormAuthor")
    public List<Admin> getOrderFormAuthor(Admin loginAgent) {
        return businessService.getOrderFormAuthor(loginAgent);
    }

    @ResponseBody
    @RequestMapping("/deleteProcessInstanceCascade")
    public void deleteProcessInstanceCascade(String executionId) {
        businessService.deleteProcessInstanceCascade(executionId);
    }

    @ResponseBody
    @RequestMapping("/productionAudit")
    public void productionAudit(String taskId, boolean success, String remarks,
                                @ModelAttribute("user") Admin user) {
        businessService.productionAudit(taskId, success, remarks, user);
    }

    @ResponseBody
    @RequestMapping("/sampleConfim")
    public void sampleConfim(String taskId, boolean success, String remarks,
                             @ModelAttribute("user") Admin user) {
        businessService.sampleConfim(taskId, success, remarks, user);
    }

    @ResponseBody
    @RequestMapping("/agentAudit")
    public void agentAudit(String taskId, boolean success, String remarks,
                           @ModelAttribute("user") Admin user) {
        businessService.agentAudit(taskId, success, remarks, user);
    }

    @ResponseBody
    @RequestMapping("/moulderAudit")
    public void moulderAudit(String taskId, boolean success, String remarks,
                             @ModelAttribute("user") Admin user) {
        businessService.moulderAudit(taskId, success, remarks, user);
    }

    @ResponseBody
    @RequestMapping("/stopOrderFlow")
    public void stopOrderFlow(Integer orderId) {
        businessService.stopOrderFlow(orderId);
    }

    @ResponseBody
    @RequestMapping("/finishPaichan")
    public void finishPaichan(Integer orderId){
        businessService.finishPaichan(orderId);
    }

    @ResponseBody
    @RequestMapping("/restartOrderFlow")
    public void restartOrderFlow(Integer orderId) {
        businessService.restartOrderFlow(orderId);
    }

    @ResponseBody
    @RequestMapping("/getOrderFormInfo")
    public OrderForm getOrderFormInfo(String taskId) {
        return businessService.getOrderFormInfo(taskId);
    }

    @ResponseBody
    @RequestMapping("/deployFlow")
    public String deployFlow() {
        return businessService.deployFlow();
    }


    @ResponseBody
    @RequestMapping("/ProcessInstance")
    public ProcessInstance findProcessInstanceByKey(String id) {
        return businessService.findProcessInstanceByKey(id);
    }

    @ResponseBody
    @RequestMapping("/deleteOrder")
    public void deleteOrder(Integer orderId) {
        businessService.deleteOrder(orderId);
    }

    @ResponseBody
    @RequestMapping("/delete")
    public void delete(OrderForm orderForm) {
        businessService.delete(orderForm);
    }

    @ResponseBody
    @RequestMapping("/paiChan")
    public void paiChan(String orderId, String formData, String orderDetailData) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            }.getType());
        List<PaichanOrderDetail> paichanOrderDetailList = JSONObject.parseObject(orderDetailData,
            new TypeReference<List<PaichanOrderDetail>>() {
            }.getType());
        businessService.addpaiChan(orderId, form, paichanOrderDetailList);
    }

}
